package cn.wang.service.cargo.packing.Impl;

import cn.wang.dao.BaseDao;
import cn.wang.entity.cargo.export.Export;
import cn.wang.entity.cargo.packing.PackingList;
import cn.wang.entity.sysadmin.User;
import cn.wang.service.cargo.packing.PackingListService;
import cn.wang.utils.Pagination;
import cn.wang.utils.UtilFuns;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by john on 2017/11/1.
 */
@Service("packingListService")
@Transactional
public class PackingListServiceImpl implements PackingListService {
    @Autowired
    private BaseDao<PackingList, String> baseDao;
    @Autowired
    private BaseDao<Export, String> exportBaseDao;
    @Autowired
    private BaseDao<User, String> userdao;

    @Override
    public Pagination findPage(int pageNo, int pageSize, long l) {

        return baseDao.pageByHql("from PackingList where state=?", pageNo, pageSize,l);
    }

    @Override
    public void save(PackingList packingList) {
      /*1设置装箱单中报运单id 合同号
          exportIds; 逗号隔开
          exportNos; 报运单号 空格隔开

        2 根据export id修改报运单state为2
        3 保存装箱单
       */
        //将数id组转分割
        //将数组转为字符串 "id1","id2"
        String exportIds = packingList.getExportIds();
        String[] epids = exportIds.split(", ");
        String id = UtilFuns.joinInStr(epids);
        List<Export> exportList = exportBaseDao.getListByHQL("from Export where id in(" + id + ")");
        String ids = "";
        String Nos = "";
        for (int i = 0; i < exportList.size(); i++) {
            Export export = exportList.get(i);
            ids += export.getId() + ",";
            if (export.getCustomerContract() != null) {
                Nos += export.getCustomerContract() + " ";
            }
            export.setState(2);
            exportBaseDao.update(export);
        }

        packingList.setExportIds(ids);
        packingList.setExportNos(Nos);
        //设置创建日期
        packingList.setCreateTime(new Timestamp(System.currentTimeMillis()));
        //创建人 创见部门 从session中获取用户
        User user = (User) ServletActionContext.getRequest().getSession().getAttribute("_CURRENT_USER");
        //User user1=userdao.get(User.class,user.getId());
        packingList.setCreateBy(user.getId());
        packingList.setCreateDept(user.getDept().getId());
        packingList.setState(0l);
        baseDao.save(packingList);
    }

    @Override
    public void update(PackingList model) {
        //查询出老数据
        PackingList packingList = baseDao.get(PackingList.class, model.getId());
        packingList.setSeller(model.getSeller());
        packingList.setBuyer(model.getBuyer());
        packingList.setInvoiceNo(model.getInvoiceNo());
        packingList.setMarks(model.getMarks());
        packingList.setDescriptions(model.getDescriptions());
        packingList.setInvoiceDate(model.getInvoiceDate());
        baseDao.update(packingList);
    }

    @Override
    public PackingList findById(String id) {
        return baseDao.get(PackingList.class, id);
    }

    @Override
    public void delete(String[] ids) {
        //业务层放到service 保证在同一事务
        //删除前设置报运单状态为1
        for (String id : ids) {
            String epIds = baseDao.get(PackingList.class, id).getExportIds();
            if (epIds != null) {
                String[] exprotid = epIds.split(",");
                for (String epid : exprotid) {
                    Export export = exportBaseDao.get(Export.class, epid);
                    export.setState(1);
                    exportBaseDao.update(export);

                }
            }

            baseDao.deleteById(PackingList.class, id);
        }
    }

    @Override
    public void submit(String[] ids) {
        for (String id : ids) {
           PackingList packingList = baseDao.get(PackingList.class, id);
            if (packingList != null) {
                packingList.setState(1l);
                baseDao.update(packingList);
                }
            }
    }

    @Override
    public List<PackingList> findBydeliveryPeriod(String date) {

        return baseDao.getListByHQL("from PackingList c where to_char(c.deliveryPeriod,'yyyy-mm-dd')=?", date);
    }

}
